import java.sql.*;
import java.util.HashMap;
import java.util.Properties;

/**
 * 用于对某个学号做运算
 */
public class ProcessOn2019 {
    Date specialCanteenD,lastCanteenD;//特别食堂日 上次食堂日
    int CanteenCount;//一直没去食堂天数
    String specialCanteen;//特别食堂号
    int sum;//早饭总天数
    int serialSum;//连续早饭总天数

    HashMap<String,Integer> mealPlace=new HashMap<>();
    int count;//计数器
    int isntSerial;//连续中断

    public ProcessOn2019() throws Exception {
    }

    public MySet process(String id) throws Exception{
        ready();
        ResultSet resultSet=getOnesResultSet(id);
        MySet mySet=new MySet();
        mySet.id=id;

        Date specialShopD=new Date(0);
        Date earliestDate=new Date(0),lastBreakDate;//最早早饭日期 上次早饭日期
        String latestLoc="0"; //最晚晚饭食堂
        specialCanteen="0";//特别食堂号
        specialCanteenD=new Date(0);lastCanteenD=new Date(0);//特别食堂日 上次食堂日
        String special_shop_name="0";

        double specialShopA=0.0;
        CanteenCount=0;
        lastCanteenD=new Date((long)24*60*60*1000*(365*49+11));//2018 12 31
        int breakfastA8=0;//八点前早餐数
        Time earliestTime=new Time(2*3600*1000);//最早早饭
        Time latestTime=new Time(8*3600*1000);//最晚晚饭
        lastBreakDate=new Date((long)24*60*60*1000*(365*49+11));//2018 12 31
        int[] regularTime=new int[]{0,0,0,0};//通常早饭时间
        sum=0;//
        serialSum=0;
        int printSum=0;//打印店次数
        int[] mealSum=new int[15];//各食堂次数
        int[][] marketSum=new int[5][8];//各超市次数
        double[] marketSpend=new double[5];//各超市消费
        int[] dinnerTime=new int[7];//一般晚餐时间

        count=1;
        isntSerial=0;
        int lastMode=0;
        Date tmpDate,lastD=new Date((long)24*60*60*1000*(365*49+11));//这天 上一天
        Time tmpTime,lastT=new Time(0);
        int location;
        String locationS,type;
        while (resultSet.next()){
            type=resultSet.getString(5);
            if(!(type.compareTo("15")==0||type.compareTo("1X")==0||type.compareTo("99")==0
                    ||type.compareTo("94")==0||type.compareTo("1D")==0||type.compareTo("1T")==0
                    ||type.compareTo("18")==0||type.compareTo("27")==0||type.compareTo("21")==0
                    ||type.compareTo("78")==0||type.compareTo("83")==0)){
                continue;
            }
            locationS=resultSet.getString(4);
            location=resultSet.getInt(4);
            tmpDate=resultSet.getDate(6);
            tmpTime=resultSet.getTime(6);
            int mealMode=judge(location,tmpTime);
            if(mealMode==1&&tmpTime.before(earliestTime)){
                earliestDate=tmpDate;
                earliestTime=tmpTime;
            }
            if(mealMode==5&&tmpTime.after(latestTime)){
                latestLoc=locationS;
                latestTime=tmpTime;
            }
            if(mealMode==1){
                judgeBreak(tmpDate,lastBreakDate);
                if(tmpDate.getTime()!=lastBreakDate.getTime()) {
                    if(tmpTime.getTime()<0)breakfastA8++;
                    if (tmpTime.getTime() < (-1 * 3600 * 1000 - 15 * 60 * 1000)) regularTime[0]++;
                    else if (tmpTime.getTime() < (-15 * 60 * 1000)) regularTime[1]++;
                    else if (tmpTime.getTime() < (3600 * 1000 - 15 * 60 * 1000)) regularTime[2]++;
                    else regularTime[3]++;
                }
                lastBreakDate=tmpDate;
            }
            if(mealMode==1||mealMode==2||mealMode==5){
                judgeCanteen(tmpDate,lastCanteenD,locationS);
            }
            if(mealMode==4){
                if((resultSet.getDouble(7))*-1>specialShopA){
                    specialShopA=(resultSet.getDouble(7))*-1;
                    specialShopD=tmpDate;
                    special_shop_name=locationS;
                }
                marketSum[mealPlace.get(locationS)][0]++;
                marketSpend[mealPlace.get(locationS)]-=resultSet.getDouble(7);
                if(tmpTime.getTime()<0){
                    marketSum[mealPlace.get(locationS)][1]++;
                }
                else if(tmpTime.getTime()<3*3600*1000){
                    marketSum[mealPlace.get(locationS)][2]++;
                }
                else if(tmpTime.getTime()<5*3600*1000){
                    marketSum[mealPlace.get(locationS)][3]++;
                }
                else if(tmpTime.getTime()<9*3600*1000){
                    marketSum[mealPlace.get(locationS)][4]++;
                }
                else if(tmpTime.getTime()<11*3600*1000){
                    marketSum[mealPlace.get(locationS)][5]++;
                }
                else if(tmpTime.getTime()<15*3600*1000){
                    marketSum[mealPlace.get(locationS)][6]++;
                }
                else if(tmpTime.getTime()<16*3600*1000){
                    marketSum[mealPlace.get(locationS)][7]++;
                }
            }
            if((tmpDate.getTime() - lastD.getTime() == 0 && tmpTime.getTime() - lastT.getTime() < 3600 * 1000)
                    && (((mealMode==1||mealMode==2||mealMode==5)&&(lastMode==1||lastMode==2||lastMode==5))||(mealMode==3&&lastMode==3)||(mealMode==4&&lastMode==4)));
            else {
                if(mealMode==3){
                    printSum++;
                }
                else if(mealMode==4){

                }
                else if(mealMode==5){
                    mealSum[mealPlace.get(locationS)]++;
                    if(tmpTime.getTime()<3600*8750){
                        dinnerTime[0]++;
                    }
                    else if(tmpTime.getTime()<3600*9750){
                        dinnerTime[1]++;
                    }
                    else if(tmpTime.getTime()<3600*10750){
                        dinnerTime[2]++;
                    }
                    else if(tmpTime.getTime()<3600*11750){
                        dinnerTime[3]++;
                    }
                    else if(tmpTime.getTime()<3600*12750){
                        dinnerTime[4]++;
                    }
                    else if(tmpTime.getTime()<3600*13625){
                        dinnerTime[5]++;
                    }
                    else if(tmpTime.getTime()<=3600*14000){
                        dinnerTime[6]++;
                    }
                }
                else if(mealMode!=0){
                    mealSum[mealPlace.get(locationS)]++;
                }
            }

            lastD=tmpDate;
            lastT=tmpTime;
            lastMode=mealMode;
        }
        mySet.special_shop_name=mealPlace.get(special_shop_name);
        mySet.specialCount=CanteenCount;
        mySet.specialCanteenD=specialCanteenD;
        mySet.specialCanteen=mealPlace.get(specialCanteen);
        mySet.earliestDate=earliestDate;
        mySet.earliestTime=earliestTime;
        mySet.latestLoc=mealPlace.get(latestLoc);
        mySet.latestTime=latestTime;
        mySet.regularTime=regularTime;
        mySet.sum=sum;
        mySet.serialSum=serialSum;
        mySet.dinnerTime=dinnerTime;
        mySet.mealSum=mealSum;
        mySet.breakfastA8=breakfastA8;
        mySet.printSum=printSum;
        mySet.marketSum=marketSum;
        mySet.marketSpend=marketSpend;

        mySet.specialShopA=specialShopA;
        mySet.specialShopD=specialShopD;
        //mySet.printAll();
        //connection.close();

        resultSet.close();//回收以防内存超出
        return mySet;
    }
    private void judgeCanteen(Date today,Date last,String locS){
        long time=(today.getTime()-last.getTime())/(24*60*60*1000);
        if(today.getTime()>=(long)24*60*60*1000*(365*49+11+31+21)&&last.getTime()<=(long)24*60*60*1000*(365*49+11+25)){
            last=new Date((long)24*60*60*1000*(365*49+11+31+22));
            time=(today.getTime()-last.getTime())/(24*60*60*1000);
        }
        if(today.getTime()>=(long)24*60*60*1000*(365*49+245)&&last.getTime()<=(long)24*60*60*1000*(365*49+200+12)){// 7-20 8-22
            last=new Date((long)24*60*60*1000*(365*49+248));//8-25
            time=(today.getTime()-last.getTime())/(24*60*60*1000);
        }
        if(time>CanteenCount){
            specialCanteenD=today;
            specialCanteen=locS;
            CanteenCount=(int)time;
        }
        lastCanteenD=today;
    }
    private void judgeBreak(Date today,Date last){
        long time=(today.getTime()-last.getTime())/(24*60*60*1000);
        if(time==0)return;
        if(time==1){
            sum++;
            if(++count==3)serialSum+=3;
            else if(count>3)serialSum++;
            if(isntSerial!=0)isntSerial--;
        }
        else if(time==2){
            sum++;
            if(count>1) {
                if (isntSerial == 0) isntSerial = 5;
                else {
                    count = 1;
                    isntSerial = 0;
                }
            }
        }
        else if(time>2){
            sum++;
            count=1;
            isntSerial=0;
        }
        //print(today+" "+" "+serialSum+" "+"\n");
    }
    private int judge(int location,Time time){
        if((location>=5020101&&location<=5020106)||(location>=5020201&&location<=5020204)||(location>=5020206&&location<=5020208)||(location>=2010101&&location<=2010102)) {
            if ((time.getTime() >= -7200000 && time.getTime() <= 6900000)||(time.getTime() >= 79200000 && time.getTime() <= 93300000)) {
                return 1;
            }
            else if ((time.getTime() >= 8*3600*1000 && time.getTime() <= 14*3600*1000)) {
                return 5;
            }
            else {
                return 2;
            }
        }
        else if(location==5020702||location==5030002||location==5100001||location==5991002||location==5991006||
                location==5992006||location==5992007)return 3;
        else if(location==5991001||location==5991003||location==5991007||location==5992001|| location==5992004)return 4;//
        return 0;
    }

    Connection connection=getConn();
    private ResultSet getOnesResultSet(String id) throws Exception{
        String sql = "SELECT *  from ecard2019.new_transaction where XGH='"+id+"' order by LSSJ";
        PreparedStatement PSN = connection.prepareStatement(sql);
        return PSN.executeQuery();
    }

    private Connection getConn() throws Exception{
        String driver = "com.mysql.jdbc.Driver";
        String url;
//            url = "jdbc:mysql://sends-2020-ecard.mysql.polardb.rds.aliyuncs.com:3306/ecard_hqu_sends?useSSL=false";
        url = "jdbc:mysql://127.0.0.1:3306/ecard2019?useSSL=false&allowMultiQueries=true&serverTimezone=UTC";

        Properties props = new Properties();
        props.put("user", "root");
        props.put("password", "root");

        Class.forName(driver);
        return DriverManager.getConnection(url,props);
    }
    void ready() {
        int i=0;
        mealPlace.put("2010101",i++); //参照business info食堂编号
        mealPlace.put("2010102",i++);
        //mealPlace.put("2010103",i++);

        mealPlace.put("5020101",i++);
        mealPlace.put("5020102",i++);
        mealPlace.put("5020103",i++);
        mealPlace.put("5020104",i++);
        mealPlace.put("5020105",i++);
        mealPlace.put("5020106",i++);
        //mealPlace.put("5020107",i++);
        //mealPlace.put("5020108",i++);

        mealPlace.put("5020201",i++);
        mealPlace.put("5020202",i++);
        mealPlace.put("5020203",i++);
        mealPlace.put("5020204",i++);
        //mealPlace.put("5020205",i++);
        mealPlace.put("5020206",i++);
        mealPlace.put("5020207",i++);
        mealPlace.put("5020208",i);
        mealPlace.put("0",99);
        //mealPlace.put("5020209",i++);

        //mealPlace.put("5020301",i++);

        //mealPlace.put("5050001",i);
        //
        i=0;
        //mealPlace.put("5060001",i++);//参照business info超市编号
        mealPlace.put("5991001",i++);
        mealPlace.put("5991003",i++);
        //mealPlace.put("5991005",i++);
        mealPlace.put("5991007",i++);
        mealPlace.put("5992001",i++);
        //mealPlace.put("5992002",i++);
        //mealPlace.put("5992003",i++);
        mealPlace.put("5992004",i);
        //mealPlace.put("5992005",i++);
        //mealPlace.put("5992008",i++);
        //mealPlace.put("5992010",i);
    }
}
